package lianxi06;

import java.util.ArrayList;
import java.util.Collections;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86131
 * Date: 2024-04-21
 * Time: 17:03
 */
public class MyThread extends Thread{
    ArrayList<Integer> list;

    public MyThread(ArrayList<Integer> list) {
        this.list = list;
    }

    //线程1
    static ArrayList<Integer> list1 = new ArrayList<>();
    //线程2
    static ArrayList<Integer> list2 = new ArrayList<>();

    @Override
    public void run() {
        //1.循环
        //2.同步代码块
        //3.循环(到末尾)
        //4.循环(没有到末尾)
        while(true){
            synchronized (MyThread.class) {
                if (list.size() == 0){  //到末尾
                    if("抽奖箱1".equals(getName())) {
                        System.out.println("抽奖箱1"+list1);
                    } else {
                        System.out.println("抽奖箱2"+list2);
                    }
                    break;
                }else {  //没有到末尾
                    Collections.shuffle(list);  //打乱集合
                    int price = list.remove(0);
                    if("抽奖箱1".equals(getName())) {
                        list1.add(price);
                    } else {
                        list2.add(price);
                    }
                }

            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
